﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RockBus.Utilities
{
    public static class LinqExtensions
    {
        public static bool IsNullOrEmpty<T>(this IEnumerable<T> items)
        {
            return ((items == null) || (!items.Any()));
        }

        /// <summary>
        /// Uses Environment.NewLine as seperator
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <returns></returns>
        public static string EnumerableToString<T>(this IEnumerable<T> items)
        {
            return EnumerableToString(items, Environment.NewLine);
        }

        public static string EnumerableToString<T>(this IEnumerable<T> items, string seperator)
        {
            var sb = new StringBuilder();
            foreach (var item in items)
            {
                sb.Append(item.ToString());
                sb.Append(seperator);
            }
            return sb.ToString();
        }

        public static IEnumerable<T> Clone<T>(this IEnumerable<T> items)
            where T : class, ICloneable
        {
            var clonedItems = new List<T>();
            foreach (T item in items)
            {
                var clone = item.Clone() as T;
                clonedItems.Add(clone);
            }
            return clonedItems;
        }
    }
}